package com.kalix.mooc.reply.biz;

import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.impl.biz.ShiroGenericBizServiceImpl;
import com.kalix.mooc.reply.api.biz.IMessageBeanService;
import com.kalix.mooc.reply.api.biz.IMessageReplyBeanService;
import com.kalix.mooc.reply.api.dao.IMessageBeanDao;
import com.kalix.mooc.reply.api.dto.AllMessageDTO;
import com.kalix.mooc.reply.api.dto.DiscussJsonBean;
import com.kalix.mooc.reply.api.dto.MessageDTO;
import com.kalix.mooc.reply.api.dto.MessageJsonBean;
import com.kalix.mooc.reply.entities.DiscussBean;
import com.kalix.mooc.reply.entities.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:
 *
 * @Description: 留言实现类
 * @Author: yyp
 * @CreateDate: 2019/5/28$ 15:40$
 * @Version:
 */
public class MessageBeanServiceImpl extends ShiroGenericBizServiceImpl<IMessageBeanDao, MessageBean> implements IMessageBeanService {
    private IMessageReplyBeanService iMessageReplyBeanService;

    /**
     * 根据cid查询所有留言
     *
     * @param id
     * @return
     */
    @Override
    public JsonData getMesByCid(long id) {
        String sql = "Select m.id,m.cid,m.content,m.score,m.creationDate,u.icon,u.name as uname from mooc_message m,sys_user u where m.uid = u.id and  m.cid = " + id + " order by m.creationDate desc";
        List<MessageDTO> list = dao.findByNativeSql(sql, MessageDTO.class);
        JsonData jsonData = new JsonData();
        jsonData.setData(list);
        jsonData.setTotalCount((long) list.size());
        return jsonData;
    }

    public JsonData getAllMessage(Integer page, Integer limit, String jsonStr,String cid) {
        String sql = "Select m.id,m.cid,m.uid,c.name as cname,m.content,m.score,c.did,m.username,u.icon,m.creationDate from mooc_message m, mooc_course c,sys_user u where m.cid=c.id and u.id = m.uid ";
        //  String sql = "select *from mooc_message";
        if (!cid.isEmpty() && cid!=null) {
            sql += " and m.cid=" + cid;
        }
        List<AllMessageDTO> list = dao.findByNativeSql(sql, AllMessageDTO.class);
        JsonData jsondata = new JsonData();
        if (list.size() > 0) {
            if (page != null && limit != null) {
                List<AllMessageDTO> mlist = list.subList(page * limit - limit, limit * page > list.size() ? list.size() : limit * page);
                jsondata.setData(mlist);
                jsondata.setTotalCount((long) list.size());
            } else {
                jsondata.setData(list);
                jsondata.setTotalCount((long) list.size());
            }
        }
        return jsondata;

    }

    /**
     * 回复管理左侧菜单 2019/05/22
     */
    @Override
    public JsonData getMessageReplyForTree() {
       // List<DiscussJsonBean> list = new ArrayList<>();
        List<MessageJsonBean> list = new ArrayList<>();
        List<MessageBean> modelList = dao.find("select f from MessageBean f order by f.id desc");
        if (modelList.size() > 0) {
            for (MessageBean messageBean : modelList) {
                MessageJsonBean messageJsonBean = new MessageJsonBean();
                messageJsonBean.setValue(messageBean.getId());
                messageJsonBean.setLabel(messageBean.getContent());
                messageJsonBean.setReplyMessage(messageBean.getContent());
                list.add(messageJsonBean);
            }
        }
        JsonData jsonData = new JsonData();
        jsonData.setData(list);
        jsonData.setTotalCount((long) list.size());
        return jsonData;
    }


    public IMessageReplyBeanService getiMessageReplyBeanService() {
        return iMessageReplyBeanService;
    }

    public void setiMessageReplyBeanService(IMessageReplyBeanService iMessageReplyBeanService) {
        this.iMessageReplyBeanService = iMessageReplyBeanService;
    }

    @Override
    public void deleteAllById(long id) {
        dao.remove(id);
        iMessageReplyBeanService.deleteAllByPostid(id);
    }
}
